/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package eu.anorien.dmo;

import eu.anorien.dmo.proxy.Proxy;
import gnu.cajo.utils.extra.TransparentItemProxy;
import java.net.Inet4Address;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.log4j.Logger;

/**
 *
 * @author David Miguel Antunes <davidmiguel [ at ] antunes.net>
 */
public class Server {

    private static final Logger logger = Logger.getLogger(Server.class);
    private Proxy instance;
    private String id;
    private String hostname;
    private int port;
    private String password;
    private boolean reachable = false;

    public Server(String id, String hostname, int port, String password) {
        this();
        this.id = id;
        this.hostname = hostname;
        this.port = port;
        this.password = password;
    }

    public Server() {
        new Timer().schedule(new TimerTask() {

            @Override
            public void run() {
                try {
                    getProxy().ping();
                    reachable = true;
                } catch (Exception e) {
                    reachable = false;
                }
            }
        }, 0, 1000);
    }

    private void connectToProxy() throws Exception {
        try {
            instance = (Proxy) TransparentItemProxy.getItem(
                    "//" + Inet4Address.getByName(hostname).getCanonicalHostName() + ":"
                    + port + "/"
                    + Proxy.class.getSimpleName(),
                    new Class[]{Proxy.class});
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new Exception("Cannot connect to " + Proxy.class.getSimpleName());
        }
    }

    public synchronized Proxy getProxy() throws Exception {
        if (instance == null) {
            connectToProxy();
        }
        try {
            if (!Inet4Address.getByName(hostname).isReachable(1000)) {
                throw new Exception("Host " + hostname + " is unreachable");
            }
            instance.ping();
        } catch (Exception e) {
            connectToProxy();
        }
        return instance;
    }

    public boolean isReachable() {
        return reachable;
    }

    public synchronized String getHostname() {
        return hostname;
    }

    public synchronized void setHostname(String hostname) {
        this.hostname = hostname;
        instance = null;
    }

    public synchronized String getId() {
        return id;
    }

    public synchronized void setId(String id) {
        this.id = id;
    }

    public synchronized int getPort() {
        return port;
    }

    public synchronized void setPort(int port) {
        this.port = port;
        instance = null;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public synchronized String toString() {
        return hostname + ":" + port;
    }
}
